package com.xzh.Dao;

import com.xzh.Entity.User;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;


import java.util.List;

public class UserDaoImpl implements  UserDao{
    private HibernateTemplate ht=null;
    private SessionFactory sessionFactory=null;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public HibernateTemplate getHibernateTemplate() {
        if(ht==null){
            ht=new HibernateTemplate(sessionFactory);
        }
        return ht;
    }
    public User get(Integer id){
        return getHibernateTemplate().get(User.class,id);
    }
    public Integer save(User user){
        return  (Integer)getHibernateTemplate().save(user);
    }

    @Override
    public List<User> findByName(String name) {
        return null;
    }

    public List<User>findByNmae(String name){
        return  (List<User>)getHibernateTemplate().find("from User u where u.name=?",name);
    }

    /*public void setSessionFactory(LocalSessionFactoryBean sessionFactory) {
        this.sessionFactory = (SessionFactory) sessionFactory;
    }*/
}
